﻿using EntityModels.Entities;
using Microsoft.EntityFrameworkCore;

namespace EntityModels.DbContexts
{
    /// <summary>
    /// 继承
    /// </summary>
    public class InheritEntityDbContext : DbContext
    {
        public DbSet<ParentEntity> ParentEntities => Set<ParentEntity>();
        public DbSet<SonEntity1> SonEntity1s => Set<SonEntity1>();
        public DbSet<SonEntity2> SonEntity2s => Set<SonEntity2>();
        public DbSet<GrandsonEntity1_1> GrandsonEntity1_1s => Set<GrandsonEntity1_1>();
        public DbSet<GrandsonEntity1_2> GrandsonEntity1_2s => Set<GrandsonEntity1_2>();
        public DbSet<NoBaseEntity> NoBaseEntities => Set<NoBaseEntity>();

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ParentEntity>()
                .HasDiscriminator(p => p.EntityType)
                .HasValue<ParentEntity>("p")
                .HasValue<SonEntity1>("s1")
                .HasValue<SonEntity2>("s2")
                .HasValue<GrandsonEntity1_1>("gs1_1")
            .HasValue<GrandsonEntity1_2>("gs1_2");

            modelBuilder.Entity<GrandsonEntity1_1>()
            .Property(g => g.GrandsonNumber)
            .HasColumnName("GrandsonNumber");

            modelBuilder.Entity<GrandsonEntity1_2>()
                .Property(g => g.GrandsonNumber)
                .HasColumnName("GrandsonNumber");

            modelBuilder.Entity<NoBaseEntity>()
                .HasBaseType((Type)null);

        }

    }
}
